package dm1101.demo02;

import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/11/1 15:08
 */
public class InstantLockTest {

    public static void main(String[] args) {
        MyTicket myTicket = new MyTicket();


        Thread t1 = new Thread(myTicket,"窗口A");
        Thread t2 = new Thread(myTicket,"窗口B");
        Thread t3 = new Thread(myTicket,"窗口C");
        Thread t4 = new Thread(myTicket,"窗口D");

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }
}
class MyTicket implements Runnable{
    private int ticket=1000;
    private static Object lock = new Object();
    @Override
    public void run() {
        while (true){
            synchronized (lock) {
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + "卖了一张票，剩余:" + --ticket);
                } else {
                    System.out.println("票已售空，请明天再来！！");
                    break;
                }
            }
        }
    }
}